package nckd.wnqjt.tmc.tda.plugin.form;

import kd.bos.isc.util.dt.D;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.temporal.ChronoUnit;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

    /**
     * 获取传入年月第一天
     */
    public static Date getMinMonth(Integer index){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat strFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat endFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.setTime(getOldMonth());
        calendar.add(Calendar.MONTH, index+1);

        //获取第一天
        int fist = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, fist);
        String str = null;
        Date data = null;
        try {
            str = strFormat.format(calendar.getTime());
            str = str + " 00:00:00";
            data = endFormat.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return data;
    }


    public static String getMonthName(Integer index){
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM");
        calendar.setTime(getOldMonth());
        calendar.add(Calendar.MONTH, index+1);
        return format.format(calendar.getTime());
    }


    /**
     * 获取传入年月最后一天
     */
    public static Date getMaxMonth(Integer index) {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat strFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat endFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        calendar.setTime(getOldMonth());
        calendar.add(Calendar.MONTH, index+1);
        //获取第一天
        int last = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        calendar.set(Calendar.DAY_OF_MONTH, last);
        String str = null;
        Date data = null;
        try {
            str = strFormat.format(calendar.getTime());
            str = str + " 23:59:59";
            data = endFormat.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return data;
    }


    /**
     * 日期计算(计算两个日期区间)
     * @param str
     * @param end
     * @return
     */
    public static Long getDateResult(Date str , Date end){

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Calendar strCalender = Calendar.getInstance();
        strCalender.setTime(str);

        Calendar endCalender = Calendar.getInstance();
        endCalender.setTime(end);
        endCalender.add(Calendar.YEAR, -1);

        //int i = endCalender.get(Calendar.DATE) - strCalender.get(Calendar.DATE);

        String strDate = simpleDateFormat.format(str);
        String endDate = simpleDateFormat.format(endCalender.getTime());
        LocalDate strTime = LocalDate.parse(strDate);
        LocalDate endTime = LocalDate.parse(endDate);
        return ChronoUnit.DAYS.between(strTime, endTime);
    }

    /**
     * 获取当前月份最后一天
     */
    public static Date getNowMonthToDate() {
        Calendar calendar = Calendar.getInstance();
        //设置当前月份最后一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return calendar.getTime();
    }

    /**
     * 获取当前月份第一天
     */
    public static Date getNowMonthMinToDate() {
        Calendar calendar = Calendar.getInstance();
        SimpleDateFormat strFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat endFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //设置当前月份第一天
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        String str = null;
        Date data = null;
        try {
            str = strFormat.format(calendar.getTime());
            str = str + " 00:00:00";
            data = endFormat.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return data;
    }


    /**
     * 获取当前年份
     */
    public static int getNowYear() {
        Calendar calendar = Calendar.getInstance();
        return calendar.get(Calendar.YEAR);
    }

    /**
     * 获取当前月份
     */
    public static int getNowMonth() {
        Date date = new Date();
        return date.getMonth() + 1;
    }

    /**
     *返回一年前日期
     */
    public static Date getOldMonth() {
        Calendar calendar = Calendar.getInstance();
//        calendar.setTime(new Date());
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            Date date = format.parse("2025-04-30 23:59:59");
            calendar.setTime(date);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        calendar.add(Calendar.YEAR, -1);
        return calendar.getTime();
    }

    /**
     * 获取当前日期上一个月最后一天
     */
    public static Date getLastMonth(){
        Calendar calendar=Calendar.getInstance();
        SimpleDateFormat strFormat = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat endFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        int month=calendar.get(Calendar.MONTH);
        calendar.set(Calendar.MONTH, month-1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        Date data = null;
        String str = null;
        try {
            str = strFormat.format(calendar.getTime());
            str = str + " 23:59:59";
            data = endFormat.parse(str);
        } catch (ParseException e) {
            throw new RuntimeException(e);
        }
        return data;
    }
}
